export default class Set {
    private itemObj = {};
    public add(item: any): boolean {
      if (this.itemObj[item]) {
        return false;
      } else {
        this.itemObj[item] = 1;
      }
      return true;
    }
    public remove(item: any): any {
      if (!this.itemObj[item]) {
        return false;
      } else {
        delete this.itemObj[item];
      }
      return item;
    }
    public has(item: any): boolean {
      return !!this.itemObj[item];
    }
    public clear(): boolean {
      this.itemObj = {};
      return true;
    }
    public values(): any[] {
      return Object.keys(this.itemObj);
    }
    public size(): number {
      return Object.keys(this.itemObj).length;
    }
    // 判断链表是否为空
    public isEmpty(): boolean {
      return !Object.keys(this.itemObj).length;
    }
    public toString(): string {
      return Object.keys(this.itemObj).join(" ");
    }
    // 合并集合的方法 有 并集 交集 差集
    // 并集
    public union(otherSet: Set): Set {
      var newSet = new Set();
      this.values().forEach((item) => {
        newSet.add(item);
      });
      otherSet.values().forEach((item) => {
        newSet.add(item);
      });
      return newSet;
    }
    // 交集
    public intersection(otherSet: Set): Set {
      var newSet = new Set();
      this.values().forEach((item) => {
        otherSet.has(item) && newSet.add(item);
      });
      return newSet;
    }
    // 差集
    public difference(otherSet: Set): Set {
      var newSet = new Set();
      this.values().forEach((item) => {
        !otherSet.has(item) && newSet.add(item);
      });
      return newSet;
    }
    // 子集
    public subset(otherSet: Set): boolean {
      var isSub = this.values().some((item) => {
        return !otherSet.has(item);
      });
      return !isSub;
    }
  }

